//
//  MOAlerter.m
//  WCImApp
//
//  Created by alfie on 2019/11/21.
//  Copyright © 2019 WCImApp. All rights reserved.
//

#import "MOAlerter.h"
#import "FSTextView.h"
typedef NS_ENUM(NSInteger, MOAlerterStyle) {
    MOAlerterStyleDefault, // 默认底部弹窗
    MOAlerterStyleCenter,  // 中间弹窗
    MOAlerterStyleSheet,   // 底部取消 + 列表 弹窗
    MOAlerterStyleInput,   // 输入框弹窗
    MOAlerterStyleBottomInput,   // 底部输入框弹窗
    MOAlerterStyleCustom,  // 自定义视图弹窗
};

@interface MOAlerter () <MOAlerterTableViewDelegate>

/** 整体容器 */
@property (nonatomic, strong) UIView                     *containView;

/** title */
@property (nonatomic, strong) UILabel                    *titleLb;

/** message */
@property (nonatomic, strong) UILabel                    *messageLb;

@property (nonatomic, strong) UILabel                    *tipLabel;

/** 左按钮 -- 默认按钮 */
@property (nonatomic, strong) UIButton                   *leftBtn;

/** 右按钮 */
@property (nonatomic, strong) UIButton                   *rightBtn;

/** x号按钮 */
@property (nonatomic, strong) UIButton                   *dismissBtn;

/** customView */
@property (strong, nonatomic) UIView                     *customView;
@property (assign, nonatomic) CGFloat                     customViewheight;

/** 输入框 */
@property (weak, nonatomic) UITextField                  *textField;
@property (weak, nonatomic) UITextView                  *textView;


/** tableView数据源 */
@property (strong, nonatomic) NSArray                    *titles;

/** 左按钮回调 */
@property (nonatomic, copy) MOAlerterHandler             leftAction;

/** 右按钮回调 */
@property (nonatomic, copy) MOAlerterHandler             rightAction;

/** 输入框的左按钮回调 */
@property (nonatomic, copy) MOAlerterInputHandler        inputLeftAction;

/** 输入框的右按钮回调 */
@property (nonatomic, copy) MOAlerterInputHandler        inputRightAction;

/** 底部列表弹窗的点击回调 */
@property (nonatomic, copy) MOAlerterSheetHandler        sheetAction;

/** 点击是否隐藏 */
@property (nonatomic, assign) BOOL                       touchHide;

/** 是否显示退出按钮 */
@property (nonatomic, assign) BOOL                       showDismissBtn;

/** 样式 */
@property (assign, nonatomic) MOAlerterStyle             style;

/** 动画 */
@property (assign, nonatomic) MOAlerterAnimationOptions  animationOption;

/** 记录屏幕方向 */
@property (assign, nonatomic) UIDeviceOrientation        lastOrientation;

/** 记录 Frame */
@property (assign, nonatomic) CGRect                     originalFrame;

/** 输入内容为空时，按钮的状态是否允许点击 */
@property (nonatomic, assign) BOOL            btnEnableWhenEmptyString;

/** 保存计算的高度 */
@property (nonatomic, assign) CGFloat            alerter_H;

/// 点击按钮后关闭弹窗
@property (assign, nonatomic) BOOL              clickNoHide;
@end


@implementation MOAlerter
//#define TABLE_SPACE ([[UIScreen mainScreen] bounds].size.height > [[UIScreen mainScreen] bounds].size.width ? 25 : 35)
#define TABLE_SPACE 0

static BOOL _alert_lock = NO;


#pragma mark - 初始化
+ (instancetype)alerter {
    MOAlerter *alerter = [[MOAlerter alloc] initWithFrame:(CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height))];
    [alerter addTarget:alerter action:@selector(touchHideAction) forControlEvents:(UIControlEventTouchUpInside)];
    alerter.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    alerter.supportedInterfaceOrientations = UIInterfaceOrientationMaskPortrait;
    alerter.lastOrientation = [UIDevice currentDevice].orientation;
    if (alerter.lastOrientation == UIDeviceOrientationUnknown || alerter.lastOrientation == UIDeviceOrientationFaceUp || alerter.lastOrientation == UIDeviceOrientationFaceDown) {
        alerter.lastOrientation = UIDeviceOrientationPortrait;
    }
    [[NSNotificationCenter defaultCenter] addObserver:alerter selector:@selector(orientationDidChang:) name:UIDeviceOrientationDidChangeNotification object:nil];
    return alerter;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.hideWhenLoginByOtherDevice = YES;
        [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(loginByOtherDevice) name:@"MONotificationLoginByOtherDevice" object:nil];
    }
    return self;
}

- (void)dealloc {
    if (self.style == MOAlerterStyleInput ||self.style == MOAlerterStyleBottomInput) {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
        if(self.style == MOAlerterStyleBottomInput) {
            [[NSNotificationCenter defaultCenter] removeObserver:self name:@"doneAction" object:nil];
        }
    }
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}


- (void)leftBtnAction {
    _alert_lock = NO;
    __weak typeof(self) weakSelf = self;
    if (self.style == MOAlerterStyleInput || self.style == MOAlerterStyleBottomInput) {
        if (self.inputLeftAction) {
            self.inputLeftAction(weakSelf, weakSelf.textField, weakSelf.tipLabel);
        } else {
            if (self.style == MOAlerterStyleBottomInput) {
                [self.textField endEditing:YES];
            }
            [self hide];
        }
    } else {
        if (self.allAction) {
            self.allAction(weakSelf, 0);
        }
        if (self.leftAction) {
            self.leftAction(weakSelf);
            [self hide];
        } else {
            [self hide];
        }
    }
}


- (void)rightBtnAction {
    _alert_lock = NO;
    __weak typeof(self) weakSelf = self;
    if (self.style == MOAlerterStyleInput || self.style == MOAlerterStyleBottomInput) {
        self.tipLabel.text = @"";
        if (self.inputRightAction) {
            [self.textField endEditing:YES];
            [[UIApplication sharedApplication].delegate.window endEditing:YES];
            if (weakSelf.textField) {
                self.inputRightAction(weakSelf, weakSelf.textField, weakSelf.tipLabel);
            }else if (weakSelf.textView) {
                self.inputRightAction(weakSelf, weakSelf.textView, weakSelf.tipLabel);
            }
            if (self.style == MOAlerterStyleBottomInput) {
                 [self hide];
            }
        } else {
            [self hide];
        }
    } else {
        if (self.allAction) {
            self.allAction(weakSelf, 0);
        }
        if (self.rightAction) {
            self.rightAction(weakSelf);
            if(!_clickNoHide){
                [self hide];
            }
        } else {
            if(!_clickNoHide){
                [self hide];
            }
        }
    }
}
- (void)dismissBtnAction{
    [self hide];
}


#pragma mark - UI
- (UIView *)containView {
    if (!_containView) {
        _containView = [UIView new];
        _containView.clipsToBounds = YES;
        [self addSubview:_containView];
    }
    return _containView;
}


- (UILabel *)titleLb {
    if (!_titleLb) {
        _titleLb = [UILabel new];
        _titleLb.textColor = [UIColor main_Text_color];
        _titleLb.font = WCIFontSemibold(24);
        _titleLb.numberOfLines = 0;
        _titleLb.textAlignment = NSTextAlignmentCenter;
        [self.containView addSubview:_titleLb];
    }
    return _titleLb;
}


- (UILabel *)messageLb {
    if (!_messageLb) {
        _messageLb = [UILabel new];
        _messageLb.textColor = [UIColor assist_Text_color];
        _messageLb.font = WCIFontRegular(14);
        _messageLb.numberOfLines = 0;
        _messageLb.textAlignment = NSTextAlignmentCenter;
        [self.containView addSubview:_messageLb];
    }
    return _messageLb;
}

- (UILabel *)tipLabel {
    if (!_tipLabel) {
        _tipLabel = [UILabel new];
        _tipLabel.textColor = [UIColor assist_Text_color];
        _tipLabel.font = WCIFontRegular(14);
        _tipLabel.numberOfLines = 0;
        _tipLabel.textAlignment = NSTextAlignmentCenter;
        [self.containView addSubview:_tipLabel];
    }
    return _tipLabel;
}


- (UIButton *)leftBtn {
    if (!_leftBtn) {
        _leftBtn = [UIButton new];
        [_leftBtn setTitleColor:UIColor.main_Text_color forState:(UIControlStateNormal)];
        _leftBtn.titleLabel.font = WCIFontRegular(16);
        _leftBtn.backgroundColor = [UIColor assist_Space_Color];
        [_leftBtn addTarget:self action:@selector(leftBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
        _leftBtn.layer.cornerRadius = 8.f;
        _leftBtn.layer.masksToBounds = YES;
        [self.containView addSubview:_leftBtn];
    }
    return _leftBtn;
}


- (UIButton *)rightBtn {
    if (!_rightBtn) {
        _rightBtn = [UIButton new];
        [_rightBtn setTitleColor:UIColor.whiteColor forState:(UIControlStateNormal)];
        _rightBtn.titleLabel.font = WCIFontRegular(16);
        _rightBtn.backgroundColor = [UIColor main_Color];
        _rightBtn.layer.cornerRadius = 8.f;
        _rightBtn.layer.masksToBounds = YES;
        [_rightBtn addTarget:self action:@selector(rightBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
        [self.containView addSubview:_rightBtn];
    }
    return _rightBtn;
}
- (UIButton *)dismissBtn{
    if (!_dismissBtn) {
        _dismissBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_dismissBtn setImage:[UIImage imageNamed:@"icon_close"] forState:UIControlStateNormal];
        _dismissBtn.backgroundColor = UIColor.clearColor;
        _dismissBtn.adjustsImageWhenHighlighted = NO;
        [_dismissBtn addTarget:self action:@selector(dismissBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _dismissBtn.hidden = YES;
        [self.containView addSubview:_dismissBtn];
    }
    return _dismissBtn;
}
- (void)setShowDismissBtn:(BOOL)showDismissBtn{
    _showDismissBtn = showDismissBtn;
    if (_showDismissBtn) {
        self.dismissBtn.hidden = NO;
    }else{
        self.dismissBtn.hidden = YES;
    }
}
- (void)configWithTitle:(NSString *)title message:(NSString *)message leftTitle:(NSString *)leftTitle leftAction:(MOAlerterHandler)leftAction rightTitle:(NSString *)rightTitle rightAction:(MOAlerterHandler)rightAction {
    
    //title
    if (title.length) {
        self.titleLb.text = title;
    }

    //布局
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {

         make.centerX.offset(0);
         make.centerY.offset(0);
         make.width.offset(0);
         make.height.offset(0);
    }];
    
    //message
    if (message.length) {
        self.messageLb.text = message;
         [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
                 make.top.equalTo(self.containView).offset(30);
                make.centerX.equalTo(self);
                make.width.mas_equalTo(0);
                make.height.mas_equalTo(0);
        //         make.left.offset(WCIScaleFloat(20));
        //         make.right.offset(WCIScaleFloat(-20));
            }];
    }else{
         [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.containView).offset(45);
                make.centerX.equalTo(self);
                make.width.mas_equalTo(0);
                make.height.mas_equalTo(0);
        //         make.left.offset(WCIScaleFloat(20));
        //         make.right.offset(WCIScaleFloat(-20));
            }];
    }

    //左边按钮
    if (leftTitle.length) {
        [self.leftBtn setTitle:leftTitle forState:(UIControlStateNormal)];
        self.leftAction = leftAction;
    } else if (!rightTitle.length) {
        rightTitle = @"确定";
    }

    //右边按钮
    if (rightTitle.length) {
        [self.rightBtn setTitle:rightTitle forState:(UIControlStateNormal)];
        self.rightAction = rightAction;
    }
    
    //线条
    UIView *horizantalLine = [UIView new];
    horizantalLine.backgroundColor = [UIColor assist_AlertLine_Color];
    [self.containView addSubview:horizantalLine];

    
    
    [self.dismissBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(0);
        make.right.mas_offset(0);
        make.size.mas_equalTo(CGSizeMake(44, 44));
    }];

    if (self.style == MOAlerterStyleInput) {
        
        self.tipLabel.textColor = [UIColor colorWithRed:1 green:0.31 blue:0.31 alpha:1];
        self.tipLabel.font = WCIFontRegular(12);
        self.tipLabel.textAlignment = NSTextAlignmentLeft;
        [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.messageLb.mas_bottom).offset(8);
            make.height.offset(40);
            make.left.offset(15);
            make.right.offset(-15);
        }];
        
        [self.messageLb mas_remakeConstraints:^(MASConstraintMaker *make) {

            make.top.equalTo(self.titleLb.mas_bottom).offset(8);
            make.left.right.equalTo(self.titleLb);
        }];
        
        [self.tipLabel mas_remakeConstraints:^(MASConstraintMaker *make) {

            make.top.equalTo(self.textField.mas_bottom).offset(0);
            make.left.offset(30);
            make.right.offset(-15);
            make.height.offset(16);
        }];
        
        [horizantalLine mas_makeConstraints:^(MASConstraintMaker *make) {

            make.top.equalTo(self.textField.mas_bottom).offset(32);
            make.left.right.offset(0);
            make.height.offset(0.5);
        }];
        
    } else {
        
        [self.messageLb mas_remakeConstraints:^(MASConstraintMaker *make) {

            make.top.equalTo(self.titleLb.mas_bottom).offset(8);
            make.left.right.equalTo(self.titleLb);
        }];
        
        [horizantalLine mas_makeConstraints:^(MASConstraintMaker *make) {

            make.top.equalTo(self.messageLb.mas_bottom).offset(32);
            make.left.right.offset(0);
            make.height.offset(0.5);
        }];
    }
    if(self.style == MOAlerterStyleCenter){
        _leftBtn.layer.cornerRadius = 0.f;
        _rightBtn.layer.cornerRadius = 0.f;
        _titleLb.font = WCIFontMedium(16);
        _messageLb.font = WCIFontRegular(14);
    }
    //有两个按钮
    if (leftTitle.length && rightTitle.length) {
     
     [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {

         make.left.offset(0);
         make.bottom.offset(0);
         make.top.equalTo(horizantalLine.mas_bottom).offset(0);
         make.width.equalTo(self.containView.mas_width).dividedBy(2);
         make.height.offset(45);
     }];

     [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {

         make.right.offset(0);
         make.top.width.height.equalTo(self.leftBtn);
     }];
     
     //垂直线条
     UIView *verticalLine = [UIView new];
     verticalLine.backgroundColor = [UIColor assist_AlertLine_Color];
     [self.containView addSubview:verticalLine];

     [verticalLine mas_makeConstraints:^(MASConstraintMaker *make) {

         make.top.equalTo(horizantalLine.mas_bottom);
         make.centerX.bottom.offset(0);
         make.width.offset(0.5);
     }];

    } else if (leftTitle.length && !rightTitle.length) {
     //只有左边按钮
        [self.leftBtn setTitleColor:UIColor.whiteColor forState:(UIControlStateNormal)];
        self.leftBtn.backgroundColor = [UIColor main_Color];
     [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {

//         make.width.equalTo(self.containView.mas_width).dividedBy(2);
         make.left.right.offset(0);
         make.centerX.offset(0);
         make.bottom.offset(0);
         make.top.equalTo(horizantalLine.mas_bottom).offset(0);
         make.height.offset(45);
     }];
    }
    else if (!leftTitle.length && rightTitle.length) {
     //只有右边按钮
     [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {

         make.centerX.offset(0);
         make.left.right.offset(0);
         make.bottom.offset(0);
         make.top.equalTo(horizantalLine.mas_bottom).offset(0);
         make.height.offset(45);
     }];
    }
    
}


- (void)configBottomStyleWithTitle:(NSString *)title message:(NSString *)message leftTitle:(NSString *)leftTitle leftAction:(MOAlerterHandler)leftAction rightTitle:(NSString *)rightTitle rightAction:(MOAlerterHandler)rightAction {
    
    self.leftAction = leftAction;
    self.rightAction = rightAction;
    self.titleLb.textAlignment = NSTextAlignmentLeft;
    self.messageLb.textAlignment = NSTextAlignmentLeft;
    NSString *titleStr = @"";
    NSString *messageStr = @"";
    if (!title.length) {
        title = @"提示";
    } else if (!message.length) {
        message = title;
        title = @"提示";
    }
    // title
    if ([title isKindOfClass:NSString.class]) {
        if ([(NSString *)title length]) {
            self.titleLb.text = title;
            titleStr = title;
        }
    } else if ([title isKindOfClass:NSAttributedString.class]) {
        if ([(NSAttributedString *)title length]) {
            self.titleLb.attributedText = title;
            titleStr = [self.titleLb.attributedText string];
        }
    }
    
    // message
    if ([message isKindOfClass:NSString.class]) {
        if ([(NSString *)message length]) {
            self.messageLb.text = message;
            messageStr = message;
        }
    } else if ([message isKindOfClass:NSAttributedString.class]) {
        if ([(NSAttributedString *)message length]) {
            self.messageLb.attributedText = message;
            messageStr = [self.messageLb.attributedText string];
        }
    }
    
    // 左边按钮
    if ([leftTitle isKindOfClass:NSString.class]) {
        if ([(NSString *)leftTitle length]) {
            [self.leftBtn setTitle:leftTitle forState:(UIControlStateNormal)];
        }
    } else if ([leftTitle isKindOfClass:NSAttributedString.class]) {
        if ([(NSAttributedString *)leftTitle length]) {
            [self.leftBtn setTitle:@"" forState:(UIControlStateNormal)];
            self.leftBtn.titleLabel.attributedText = leftTitle;
        }
    } else if (![rightTitle length]) {
        rightTitle = @"确定";
    }
    
    // 右边按钮
    if ([rightTitle isKindOfClass:NSString.class]) {
        if ([(NSString *)rightTitle length]) {
            [self.rightBtn setTitle:rightTitle forState:(UIControlStateNormal)];
        }
    } else if ([rightTitle isKindOfClass:NSAttributedString.class]) {
        if ([(NSAttributedString *)rightTitle length]) {
            [self.rightBtn setTitle:@"" forState:(UIControlStateNormal)];
            self.rightBtn.titleLabel.attributedText = rightTitle;
        }
    }
    
    // 布局
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.offset(32);
        make.right.offset(-32);
    }];
    
    [self.messageLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.titleLb);
        make.top.equalTo(self.titleLb.mas_bottom).offset(25);
    }];
    
    //有两个按钮
    if (_leftBtn && _rightBtn) {
     
        CGFloat left_W = [rightTitle length] == [leftTitle length] ? (UIScreen.mainScreen.bounds.size.width - 48)/2 : 113.f;
        [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.offset(16);
            make.bottom.offset(-62);
            make.width.offset(left_W);
            make.height.offset(50);
            //            make.top.equalTo(self.messageLb.mas_bottom).offset(32);
        }];
        
        [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.offset(-16);
            make.bottom.offset(-62);
            make.left.equalTo(self.leftBtn.mas_right).offset(16);
            make.height.offset(50);
            //            make.top.equalTo(self.messageLb.mas_bottom).offset(32);
        }];

    } else {
        
        UIButton *btn = _rightBtn ?: _leftBtn;
        [btn setTitleColor:UIColor.whiteColor forState:(UIControlStateNormal)];
        btn.backgroundColor = [UIColor main_Color];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.offset(32);
            make.right.offset(-32);
            make.bottom.offset(-62);
            make.height.offset(50);
            //            make.top.equalTo(self.messageLb.mas_bottom).offset(32);
        }];
    }
    
    /// 计算高度
    CGFloat msg_H = [messageStr boundingRectWithSize:CGSizeMake(UIScreen.mainScreen.bounds.size.width - 64, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : self.messageLb.font} context:nil].size.height;
    if (msg_H <= 44.f) {
        self.alerter_H = 280;
    } else {
        CGFloat title_H = [titleStr boundingRectWithSize:CGSizeMake(UIScreen.mainScreen.bounds.size.width - 64, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : self.titleLb.font} context:nil].size.height;
        self.alerter_H = title_H + msg_H + 202;
    }
}

#pragma mark - 提示弹窗

+ (instancetype)alertWithMessage:(NSString *)message confirmTitle:(NSString *)confirmTitle {
    return [self alertWithTitle:message message:nil leftTitle:nil leftAction:nil rightTitle:confirmTitle rightAction:nil touchHide:YES];
}

+ (instancetype)alertWithMessage:(NSString *)message confirmTitle:(NSString *)confirmTitle touchHide:(BOOL)touchHide {
    return [self alertWithTitle:message message:nil leftTitle:nil leftAction:nil rightTitle:confirmTitle rightAction:nil touchHide:touchHide];
}

+ (instancetype)alertWithMessage:(NSString *)message confirmTitle:(NSString *)confirmTitle confirmAction:(MOAlerterHandler)confirmAction haveClose:(BOOL)haveClose {
    MOAlerter *alerter = [MOAlerter alerter];
    alerter.style = MOAlerterStyleDefault;
    alerter.containView.backgroundColor = [UIColor assist_AlertBG_Color];
    ViewBorderRadius(alerter.containView, 16, 0, [UIColor clearColor]);
    if (haveClose) {
      alerter.touchHide = YES;
    } else {
      alerter.touchHide = NO;
    }
    alerter.showDismissBtn = haveClose;

    [alerter configWithTitle:message message:nil leftTitle:nil leftAction:nil rightTitle:confirmTitle rightAction:confirmAction];

    UIWindow *window = MOAlerter.keyWindow;
    [window addSubview:alerter];

    [MOAlerter animationWithAlerter:alerter leftTitleLength:0];
    
    return alerter;
}

+ (instancetype)alertWithTitle:(NSString *)title message:(NSString *)message leftTitle:(NSString *)leftTitle leftAction:(MOAlerterHandler)leftAction rightTitle:(NSString *)rightTitle rightAction:(MOAlerterHandler)rightAction touchHide:(BOOL)touchHide {
    return [MOAlerter alertWithTitle:title message:message leftTitle:leftTitle leftAction:leftAction rightTitle:rightTitle rightAction:rightAction touchHide:touchHide superView:nil];
}


+ (instancetype)alertWithTitle:(NSString *)title message:(NSString *)message leftTitle:(NSString *)leftTitle leftAction:(MOAlerterHandler)leftAction rightTitle:(NSString *)rightTitle rightAction:(MOAlerterHandler)rightAction touchHide:(BOOL)touchHide superView:(UIView *)superView {
    return [MOAlerter alertWithTitle:title message:message leftTitle:leftTitle leftAction:leftAction rightTitle:rightTitle rightAction:rightAction touchHide:touchHide superView:superView clickNoHide:NO];
}


+ (instancetype)alertCenterWithTitle:(NSString *)title message:(NSString *)message leftTitle:(NSString *)leftTitle leftAction:(MOAlerterHandler)leftAction rightTitle:(NSString *)rightTitle rightAction:(MOAlerterHandler)rightAction touchHide:(BOOL)touchHide superView:(UIView *)superView {
    MOAlerter *alerter = [MOAlerter alerter];
    alerter.style = MOAlerterStyleCenter;
    alerter.containView.backgroundColor = UIColor.assist_AlertBG_Color;
    ViewBorderRadius(alerter.containView, 8, 0, [UIColor clearColor]);
    alerter.touchHide = touchHide;
    alerter.showDismissBtn = NO;
    [alerter configWithTitle:title message:message leftTitle:leftTitle leftAction:leftAction rightTitle:rightTitle rightAction:rightAction];
    UIView *view = superView ?: MOAlerter.keyWindow;
    [view addSubview:alerter];
    [MOAlerter animationWithAlerter:alerter leftTitleLength:leftTitle.length];
    return alerter;
}

+ (instancetype)alertWithTitle:(NSString *)title message:(NSString *)message leftTitle:(NSString *)leftTitle leftAction:(MOAlerterHandler)leftAction rightTitle:(NSString *)rightTitle rightAction:(MOAlerterHandler)rightAction touchHide:(BOOL)touchHide superView:(UIView *)superView clickNoHide:(BOOL)clickNoHide{
    MOAlerter *alerter = [MOAlerter alerter];
    alerter.style = MOAlerterStyleDefault;
    alerter.containView.backgroundColor = UIColor.assist_AlertBG_Color;
    ViewBorderRadius(alerter.containView, 16, 0, [UIColor clearColor]);
    alerter.touchHide = touchHide;
    alerter.showDismissBtn = NO;
    alerter.clickNoHide = clickNoHide;
    [alerter configBottomStyleWithTitle:title message:message leftTitle:leftTitle leftAction:leftAction rightTitle:rightTitle rightAction:rightAction];
    UIView *view = superView ?: MOAlerter.keyWindow;
    [view addSubview:alerter];
    [MOAlerter animationWithAlerter:alerter leftTitleLength:leftTitle.length];
    return alerter;
}



+ (UIWindow *)keyWindow {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIWindow *result = window;
    for (UIWindow *subWindow in window.subviews) {
        if ([subWindow isKindOfClass:UIWindow.class] && subWindow.rootViewController && !subWindow.hidden && subWindow.alpha > 0) {
            result = subWindow;
        }
    }
    return result;
}


#pragma mark - 输入框样式的弹窗
+ (instancetype)alertInputStyleWithTitle:(NSString *)title message:(NSString *)message leftTitle:(NSString *)leftTitle leftAction:(MOAlerterInputHandler)leftAction rightTitle:(NSString *)rightTitle rightAction:(MOAlerterInputHandler)rightAction textFieldConfiguration:(void (^)(UITextField *))configuration touchHide:(BOOL)touchHide {
    
    MOAlerter *alerter = [MOAlerter alerter];
    alerter.style = MOAlerterStyleInput;
    alerter.containView.backgroundColor = UIColor.assist_AlertBG_Color;
    ViewBorderRadius(alerter.containView, 16, 0, [UIColor clearColor]);
    alerter.touchHide = touchHide;
    alerter.showDismissBtn = NO;
    [[NSNotificationCenter defaultCenter] addObserver:alerter selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:alerter selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    //输入框
    UITextField *textField = [UITextField new];
    
    textField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 15, 40)];
    textField.placeholder = [NSString stringWithFormat:(@"请输入%@"), title];
    textField.textColor = [UIColor main_Text_color];
    textField.font = WCIFontRegular(14);
    textField.clearButtonMode = UITextFieldViewModeAlways;
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.returnKeyType = UIReturnKeyDone;
    textField.backgroundColor = [UIColor assist_AlertCancelBg_Color];
    [textField addTarget:alerter action:@selector(endEditingOnExit:) forControlEvents:UIControlEventEditingDidEndOnExit];
    [textField addTarget:alerter action:@selector(changeText:) forControlEvents:UIControlEventEditingChanged];
    ViewRadius(textField, 10);
    alerter.textField = textField;
    [alerter.containView addSubview:textField];
    
    [alerter configWithTitle:title message:message leftTitle:leftTitle leftAction:nil rightTitle:rightTitle rightAction:nil];
    alerter.inputRightAction = rightAction;
    alerter.inputLeftAction = leftAction;

    if (configuration) configuration(textField);
    UIWindow *window = MOAlerter.keyWindow;
    [window addSubview:alerter];

    [MOAlerter animationWithAlerter:alerter leftTitleLength:leftTitle.length];
    
    return alerter;
}

+ (instancetype)alertBottomInputStyleWithTitle:(NSString *)title tipMessage:(NSString *)tipMessage  rightAction:(MOAlerterInputHandler)rightAction textFieldConfiguration:(void (^)(UITextField *textField))configuration touchHide:(BOOL)touchHide btnEnableWhenEmptyString:(BOOL)enable {
    MOAlerter *alerter = [MOAlerter alerter];
    alerter.style = MOAlerterStyleBottomInput;
    alerter.containView.backgroundColor = UIColor.assist_AlertBG_Color;
    alerter.containView.clipsToBounds = YES;
    alerter.touchHide = touchHide;
    alerter.showDismissBtn = NO;
    alerter.btnEnableWhenEmptyString = enable;
    alerter.animationOption = MOAlerterAnimationOptionBottom;
    [[NSNotificationCenter defaultCenter] addObserver:alerter selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:alerter selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:alerter selector:@selector(doneAction) name:@"doneAction" object:nil];
    
    [alerter.leftBtn setTitleColor:[UIColor main_Text_color] forState:UIControlStateNormal];
    alerter.leftBtn.backgroundColor = UIColor.clearColor;
    [alerter.leftBtn setTitle:(@"取消") forState:UIControlStateNormal];
    alerter.leftBtn.titleLabel.font = WCIFontRegular(16);
    [alerter.leftBtn setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [alerter.leftBtn setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    CGFloat width = alerter.leftBtn.titleLabel.intrinsicContentSize.width + 10;
    [alerter.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(16);
        make.left.mas_offset(15);
        make.width.mas_offset(width);
        make.height.mas_offset(32);
    }];
    
    [alerter.rightBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [alerter.rightBtn setTitle:@"完成" forState:UIControlStateNormal];
    [alerter.rightBtn setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [alerter.rightBtn sizeToFit];
    alerter.rightBtn.titleLabel.font = WCIFontRegular(14);
    ViewRadius(alerter.rightBtn, 8);
    alerter.rightBtn.enabled = enable;
    alerter.rightBtn.backgroundColor = enable ? [UIColor main_Color] : [UIColor colorWithLightColorStr:@"00BA95" LightAlpha:0.3 DarkColor:@"00BA95" DarkAlpha:0.3];;
    [alerter.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(16);
        make.right.mas_offset(-15);
        make.width.mas_offset(alerter.rightBtn.width + 24);
        make.height.mas_offset(32);
    }];
    
    alerter.titleLb.numberOfLines = 1;
    alerter.titleLb.font = WCIFontMedium(15);
    alerter.titleLb.text = title;
    [alerter.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(21);
        make.height.mas_offset(22);
        make.left.equalTo(alerter.leftBtn.mas_right);
        make.right.equalTo(alerter.rightBtn.mas_left);
    }];
    
    //输入框
    UITextField *textField = [UITextField new];
    textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 40)];
    textField.placeholder = [NSString stringWithFormat:(@"请输入%@"), title];
    textField.textColor = [UIColor main_Text_color];
    textField.font = WCIFontRegular(14);
    textField.clearButtonMode = UITextFieldViewModeAlways;
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.returnKeyType = UIReturnKeyDone;
    textField.backgroundColor = [UIColor assist_AlertCancelBg_Color];
    [textField addTarget:alerter action:@selector(endEditingOnExit:) forControlEvents:UIControlEventEditingDidEndOnExit];
    [textField addTarget:alerter action:@selector(changeText:) forControlEvents:UIControlEventEditingChanged];
    ViewRadius(textField, 10);
    alerter.textField = textField;
    [alerter.containView addSubview:textField];
    alerter.inputRightAction = rightAction;
    [alerter.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(64);
        make.left.mas_offset(17);
        make.right.mas_offset(-17);
        make.height.mas_offset(40);
    }];
    if (configuration) configuration(textField);
    
    if (tipMessage) {
        alerter.messageLb.text = tipMessage;
        alerter.messageLb.textColor = [UIColor assist_Text_color];
        alerter.messageLb.font = WCIFontRegular(12);
        alerter.messageLb.textAlignment = NSTextAlignmentLeft;
        [alerter.messageLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(alerter.textField.mas_bottom).offset(10);
            make.left.mas_offset(16);
            make.right.mas_offset(-16);
//            make.height.mas_offset(14);
        }];
    }
    UIWindow *window = MOAlerter.keyWindow;
    [window addSubview:alerter];
    alerter.customViewheight = tipMessage.length>0?150+WCI_IPHONE_SAFEBOTTOMAREA_HEIGHT:120+WCI_IPHONE_SAFEBOTTOMAREA_HEIGHT;
    alerter.containView.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height, SCREEN_WIDTH, alerter.customViewheight);
    [UIView setCornerRadiusWithView:alerter.containView addRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight andRadiusValue:16];
    
    [alerter.textField becomeFirstResponder];
    [UIView animateWithDuration:0.4 animations:^{
        alerter.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
//        alerter.containView.y = [[UIScreen mainScreen] bounds].size.height - alerter.containView.height;
    } completion:^(BOOL finished) {
       
    }];
    return alerter;
}


+ (instancetype)alertBottomInputTextViewWithTitle:(NSString *)title tipMessage:(NSString *)tipMessage  rightAction:(MOAlerterInputHandler)rightAction textFieldConfiguration:(void (^)(UITextView *textView))configuration touchHide:(BOOL)touchHide {
    MOAlerter *alerter = [MOAlerter alerter];
    alerter.style = MOAlerterStyleBottomInput;
    alerter.containView.backgroundColor = UIColor.assist_AlertBG_Color;
    alerter.containView.clipsToBounds = YES;
    alerter.touchHide = touchHide;
    alerter.showDismissBtn = NO;
    alerter.animationOption = MOAlerterAnimationOptionBottom;
    [[NSNotificationCenter defaultCenter] addObserver:alerter selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:alerter selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:alerter selector:@selector(doneAction) name:@"doneAction" object:nil];
    
    [alerter.leftBtn setTitleColor:[UIColor main_Text_color] forState:UIControlStateNormal];
    alerter.leftBtn.backgroundColor = UIColor.clearColor;
    [alerter.leftBtn setTitle:(@"取消") forState:UIControlStateNormal];
    alerter.leftBtn.titleLabel.font = WCIFontRegular(16);
    [alerter.leftBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
    [alerter.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(16);
        make.left.mas_offset(15);
        make.width.mas_offset(72);
        make.height.mas_offset(32);
    }];
    
    [alerter.rightBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [alerter.rightBtn setTitle:(@"发布") forState:UIControlStateNormal];
    alerter.rightBtn.titleLabel.font = WCIFontRegular(14);
    ViewRadius(alerter.rightBtn, 8);
    alerter.rightBtn.backgroundColor = main_color;
    [alerter.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(16);
        make.right.mas_offset(-15);
        make.width.mas_offset(52);
        make.height.mas_offset(32);
    }];
    
    alerter.titleLb.numberOfLines = 1;
    alerter.titleLb.font = WCIFontMedium(16);
    alerter.titleLb.text = title;
    [alerter.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(21);
        make.height.mas_offset(22);
        make.left.equalTo(alerter.leftBtn.mas_right);
        make.right.equalTo(alerter.rightBtn.mas_left);
    }];
    
    //输入框
    FSTextView *textView = [FSTextView new];
    [textView setReturnItem];
    textView.placeholderFont = [UIFont systemFontOfSize:20];
    textView.font = [UIFont systemFontOfSize:14];
    textView.placeholder = [NSString stringWithFormat:(@"请输入%@"), title];
    textView.textColor = [UIColor main_Text_color];
    textView.font = WCIFontRegular(14);
    textView.returnKeyType = UIReturnKeyDone;
    textView.backgroundColor = assist_partBg_Color;
    textView.placeholder = (@"发布后将通知所有群成员");
    
    ViewRadius(textView, 10);
    alerter.textView = textView;
    [alerter.containView addSubview:textView];
    alerter.inputRightAction = rightAction;
    [alerter.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(64);
        make.left.mas_offset(17);
        make.right.mas_offset(-17);
        make.height.mas_offset(190);
    }];
    if (configuration) configuration(textView);
    
    if (tipMessage) {
        alerter.messageLb.text = tipMessage;
        alerter.messageLb.textColor = assist_text_color;
        alerter.messageLb.font = WCIFontRegular(12);
        alerter.messageLb.textAlignment = NSTextAlignmentLeft;
        [alerter.messageLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(alerter.textField.mas_bottom).offset(10);
            make.left.mas_offset(16);
            make.right.mas_offset(-16);
            make.height.mas_offset(14);
        }];
    }
    UIWindow *window = MOAlerter.keyWindow;
    [window addSubview:alerter];
    alerter.customViewheight = tipMessage.length>0?330+WCI_IPHONE_SAFEBOTTOMAREA_HEIGHT:300+WCI_IPHONE_SAFEBOTTOMAREA_HEIGHT;
    alerter.containView.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height, SCREEN_WIDTH, alerter.customViewheight);
    [UIView setCornerRadiusWithView:alerter.containView addRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight andRadiusValue:16];
    [alerter.textView becomeFirstResponder];
    [UIView animateWithDuration:0.4 animations:^{
        alerter.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
//        alerter.containView.y = [[UIScreen mainScreen] bounds].size.height - alerter.containView.height;
    } completion:^(BOOL finished) {
       
    }];
    return alerter;
}
#pragma mark - 底部带取消样式的列表弹窗
+ (instancetype)alertSheetStyleWithTitles:(NSArray *)titles title:(NSString *)title message:(NSString *)message cancelTitle:(NSString *)cancelTitle selectedAction:(MOAlerterSheetHandler)action touchHide:(BOOL)touchHide {
    
    if (!titles.count) return nil;
    
    MOAlerter *alerter = [MOAlerter alerter];
    alerter.titles = titles;
    alerter.style = MOAlerterStyleSheet;
    alerter.touchHide = touchHide;
    alerter.animationOption = MOAlerterAnimationOptionBottom;
    alerter.sheetAction = action;
    alerter.containView.backgroundColor = UIColor.assist_AlertBG_Color;
    MOAlerterTableView *tableView = [MOAlerterTableView new];
    [tableView attachTitles:titles title:title message:message];
    tableView.alerterDelegate = alerter;
    alerter.tableView = tableView;
    [alerter.containView addSubview:tableView];
    
    //取消按钮
    if (cancelTitle.length){
        [tableView.cancelBtn setTitle:cancelTitle forState:(UIControlStateNormal)];
        [tableView.cancelBtn addTarget:alerter action:@selector(hide) forControlEvents:(UIControlEventTouchUpInside)];
        [alerter.containView addSubview:tableView.cancelBtn];
    }

    UIWindow *window = MOAlerter.keyWindow;
    UIView *lastView = window.subviews.lastObject;
    if ([lastView isKindOfClass:UIWindow.class] && [(UIWindow *)lastView rootViewController]) {
        window = lastView;
    }
    [window addSubview:alerter];
    
    NSInteger count = 0;
    for (NSString *item in titles) {
        if ([item isKindOfClass:NSString.class] && !item.length) {
            continue;;
        }
        count ++;
    }
    CGFloat table_H = fmin(55 * count+[tableView heightForHeader], [[UIScreen mainScreen] bounds].size.height - WCI_IPHONE_NAVIGATIONBAR_HEIGHT - 8 - 92 - 20);
    if (table_H == 55 * count+[tableView heightForHeader]) {
        tableView.scrollEnabled = NO;
    }else{
        tableView.scrollEnabled = YES;
    }
    CGFloat height = table_H  + 92 + 20;
    
    alerter.containView.frame = CGRectMake(TABLE_SPACE, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width - TABLE_SPACE * 2, height);
    [UIView setCornerRadiusWithView:alerter.containView addRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight andRadiusValue:16];
    //布局
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.top.offset(0);
        make.height.mas_equalTo(table_H);
    }];
    
    tableView.cancelBtn.frame = CGRectMake(32, alerter.containView.frame.size.height - 92, SCREEN_WIDTH-64, 50);
    
    [UIView animateWithDuration:0.4 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0.8 options:0 animations:^{
        
        alerter.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
        alerter.containView.y = [[UIScreen mainScreen] bounds].size.height - alerter.containView.height;
        
    } completion:nil];
    
    return alerter;
}


// tableView点击事件 MOAlerterTableViewDelegate
- (void)tableView:(MOAlerterTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    __weak typeof(self) weakSelf = self;
    if (self.sheetAction) {
        self.sheetAction(weakSelf, indexPath.row);
    }
}

#pragma mark - 自定义弹窗
+ (instancetype)alertCustomView:(UIView *)view animationOption:(MOAlerterAnimationOptions)option touchHide:(BOOL)touchHide {
    
    return [self alertCustomView:view animationOption:option toSuperView:nil touchHide:touchHide];
}

#pragma mark - 自定义弹窗

+ (instancetype)alertCustomView:(UIView *)view animationOption:(MOAlerterAnimationOptions)option toSuperView:(UIView *)superView touchHide:(BOOL)touchHide {
    
    MOAlerter *alerter = [MOAlerter alerter];
    alerter.style = MOAlerterStyleCustom;
    alerter.touchHide = touchHide;
    alerter.animationOption = option;
    alerter.customView = view;
    [alerter addSubview:view];
    
    UIView *sview = superView?:MOAlerter.keyWindow;
    [sview addSubview:alerter];
    
    switch (option) {
        case MOAlerterAnimationOptionBottom: {
            view.y = [[UIScreen mainScreen] bounds].size.height;

            [UIView animateWithDuration:0.4 delay:0 usingSpringWithDamping:0.9 initialSpringVelocity:1.2 options:0 animations:^{

                alerter.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
                view.y = [[UIScreen mainScreen] bounds].size.height - view.height;
            } completion:nil];
        }
            break;
            
        default: {
            alerter.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
            [view mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.height.offset(view.height);
                make.width.offset(view.width);
                make.centerY.equalTo(alerter);
                make.centerX.equalTo(alerter);
            }];
            [view layoutIfNeeded];
            /*
            view.alpha = 0;
            [UIView animateWithDuration:0.2 animations:^{
                alerter.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
                view.alpha = 1;
                [view mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.height.offset(view.height);
                    make.width.offset(view.width);
                    make.centerY.equalTo(alerter);
                    make.centerX.equalTo(alerter);
                }];
                [view layoutIfNeeded];
            }];
             */
        }
            break;
    }
    return alerter;
}



#pragma mark - 释放弹窗
- (void)touchHideAction {
    if (self.touchHide) {
        if (self.style == MOAlerterStyleInput ||self.style == MOAlerterStyleBottomInput) {
            [self.textField endEditing:YES];
        }
        [self hide];
    } else {
        if (self.style == MOAlerterStyleInput ||self.style == MOAlerterStyleBottomInput) {
            [self.textField endEditing:YES];
        }
    }
}


- (void)hide {
    !_touchHideHandler?:_touchHideHandler(self);
    switch (self.animationOption) {
        case MOAlerterAnimationOptionBottom: {
            [UIView animateWithDuration:0.4 delay:0 usingSpringWithDamping:0.85 initialSpringVelocity:1.2 options:0 animations:^{
                
                self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
                if (self.style == MOAlerterStyleSheet||self.style == MOAlerterStyleBottomInput) {
                    self.containView.y = [[UIScreen mainScreen] bounds].size.height;
                } else {
                    self.customView.y = [[UIScreen mainScreen] bounds].size.height;
                }
                
            } completion:^(BOOL finished) {
                _alert_lock = NO;
                if (finished) {
                    [self removeFromSuperview];
                }
            }];
        }
            break;
            
        default: {
            _alert_lock = NO;
            
            if (self.style == MOAlerterStyleDefault) {
                [UIView animateWithDuration:0.25 animations:^{
                    self.containView.frame = CGRectMake(0, UIScreen.mainScreen.bounds.size.height, UIScreen.mainScreen.bounds.size.width, self.alerter_H);
                    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
                } completion:^(BOOL finished) {
                    if (self.superview) {
                        [self removeFromSuperview];
                    }
                }];
            } else {
                if (self.superview) {
                    [self removeFromSuperview];
                }
            }
        }
            break;
    }
}

#pragma mark - Animation

+ (void)animationWithAlerter:(MOAlerter *)alerter leftTitleLength:(CGFloat)length {
    
    if (alerter.style == MOAlerterStyleCenter) {
        //动画
        CGFloat width = (fmin(UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height) - WCIScaleFloat(135));
        alerter.containView.frame = CGRectMake(UIScreen.mainScreen.bounds.size.width/2, UIScreen.mainScreen.bounds.size.height/2, 0, 0);
        [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:1 initialSpringVelocity:1 options:(UIViewAnimationOptionCurveEaseOut) animations:^{

            alerter.backgroundColor = [UIColor colorWithLightColor:[[UIColor blackColor] colorWithAlphaComponent:0.4f] DarkColor:[[UIColor blackColor] colorWithAlphaComponent:0.6f]];
            [alerter.containView mas_remakeConstraints:^(MASConstraintMaker *make) {

               make.centerX.offset(0);
               make.centerY.offset(0);
               make.width.offset(width);
               make.bottom.equalTo(length ? alerter.leftBtn : alerter.rightBtn).offset(0);
            }];
            
            [alerter.titleLb mas_remakeConstraints:^(MASConstraintMaker *make) {
                if ([alerter.messageLb.text length] > 0) {
                    make.top.equalTo(alerter.containView).offset(30);
                }else{
                    make.top.equalTo(alerter.containView).offset(45);
                }
                make.centerX.offset(0);
                make.width.mas_lessThanOrEqualTo(width - WCIScaleFloat(40));
            }];
            [alerter.containView layoutIfNeeded];
        } completion:nil];
    } else {
        alerter.containView.frame = CGRectMake(0, UIScreen.mainScreen.bounds.size.height, UIScreen.mainScreen.bounds.size.width, alerter.alerter_H);
        [UIView animateWithDuration:0.25 animations:^{
//            [alerter.containView mas_remakeConstraints:^(MASConstraintMaker *make) {
//
//               make.left.right.offset(0);
//               make.bottom.offset(0);
//               make.height.offset(280);
//            }];
//            [alerter.containView layoutIfNeeded];
            alerter.backgroundColor = [UIColor colorWithLightColor:[[UIColor blackColor] colorWithAlphaComponent:0.4f] DarkColor:[[UIColor blackColor] colorWithAlphaComponent:0.6f]];
            alerter.containView.frame = CGRectMake(0, UIScreen.mainScreen.bounds.size.height - alerter.alerter_H, UIScreen.mainScreen.bounds.size.width, alerter.alerter_H);
        }];
    }
}

#pragma mark - 键盘监听
- (void)keyboardWillShow:(NSNotification *)noti {
    double animationTimes = [noti.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue];
    CGRect keyboardFrame = [noti.userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
    
    if (self.style == MOAlerterStyleBottomInput) {
        [UIView animateWithDuration:animationTimes animations:^{

            self.containView.frame = CGRectMake(0,
                                                [[UIScreen mainScreen] bounds].size.height- self.customViewheight-keyboardFrame.size.height+WCI_IPHONE_SAFEBOTTOMAREA_HEIGHT,
                                                SCREEN_WIDTH,
                                                self.customViewheight-WCI_IPHONE_SAFEBOTTOMAREA_HEIGHT);
        }];
    }else{
        CGFloat offset = 5.f;
        UIView *responseView = self.containView;
        if (CGRectEqualToRect(self.originalFrame, CGRectZero)) {
            self.originalFrame = self.containView.frame;
        }
        CGRect responseFrame = [self convertRect:self.originalFrame fromView:responseView.superview];
        CGFloat offY = keyboardFrame.origin.y - (responseFrame.origin.y + responseFrame.size.height + offset);
        if (offY > 0) offY = 0;
        if (offY < 0) {
            [UIView animateWithDuration:animationTimes animations:^{
                [self.containView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.centerY.offset(offY);
                }];
                [self layoutIfNeeded];
            }];
        }
    }
}


- (void)keyboardWillHide:(NSNotification *)noti {
     double animationTimes = [noti.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue];
    if (self.style == MOAlerterStyleBottomInput) {
        [UIView animateWithDuration:animationTimes animations:^{
//            [self.containView mas_updateConstraints:^(MASConstraintMaker *make) {
//                make.left.right.offset(0);
//                make.height.mas_offset(self.containView.frame.size.height);
//                make.bottom.offset(0);
//            }];
//            [self layoutIfNeeded];
            self.containView.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height- self.customViewheight, SCREEN_WIDTH, self.customViewheight+WCI_IPHONE_SAFEBOTTOMAREA_HEIGHT);
        }];
    }else{
        self.originalFrame = CGRectZero;
       
        [UIView animateWithDuration:animationTimes animations:^{
            [self.containView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.centerY.offset(0);
            }];
            [self layoutIfNeeded];
        }];
    }
}

- (void)doneAction{
    [self.textField endEditing:YES];
    [self rightBtnAction];
}

#pragma mark - 点击键盘右下角
- (void)endEditingOnExit:(UITextField *)textField {
    [textField endEditing:YES];
    if (self.style == MOAlerterStyleBottomInput) {
        [self rightBtnAction];
    }
}

- (void)changeText:(UITextField *)textField {

    if (!self.btnEnableWhenEmptyString) {
        if (textField.text.length > 0) {
            self.rightBtn.enabled = YES;
            self.rightBtn.backgroundColor = [UIColor main_Color];
        } else{
            self.rightBtn.enabled = NO;
            self.rightBtn.backgroundColor = [UIColor colorWithLightColorStr:@"00BA95" LightAlpha:0.3 DarkColor:@"00BA95" DarkAlpha:0.3];;
        }
    }
/// 旧方法，不应使用tag限制长度，改成module.maxLenght
//    //限制长度 tag
//    if (textField.tag > 0) {
//        if ([textField.text length] > textField.tag) {
//            textField.text  = [self subStringWith:textField.text ToIndex:textField.tag];
//        }
//    }else {
//        if ([textField.text length] > 50) {
//            textField.text  = [self subStringWith:textField.text ToIndex:50];
//        }
//    }
}
-(NSString *)subStringWith:(NSString *)string ToIndex:(NSInteger)index{

    NSString *result = string;
    if (result.length > index) {
        NSRange rangeIndex = [result rangeOfComposedCharacterSequenceAtIndex:index];
        result = [result substringToIndex:(rangeIndex.location)];
    }

    return result;
}

#pragma mark - 屏幕旋转的通知回调
- (void)orientationDidChang:(NSNotification *)noti {

    UIDeviceOrientation orient = [UIDevice currentDevice].orientation;
    if (orient == UIDeviceOrientationFaceUp || orient == UIDeviceOrientationFaceDown) {
        return;
    }
    if (self.lastOrientation == orient) {
        return;
    }
    self.originalFrame = CGRectZero;
    self.lastOrientation = orient;
    if (self.hidesAlerterWhenOrientationDidChang) {
        [self hide];
        self.animationOption = MOAlerterAnimationOptionDefault;
        return;
    }
    if (self.supportedInterfaceOrientations == UIInterfaceOrientationMaskPortrait && self.lastOrientation == UIDeviceOrientationPortrait) {
        return;
    }
    switch (orient) {
        case UIDeviceOrientationPortrait:
            if (self.supportedInterfaceOrientations & UIInterfaceOrientationMaskPortrait) {
                self.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
            }
            break;
            
        case UIDeviceOrientationLandscapeLeft:
            if (self.supportedInterfaceOrientations & UIInterfaceOrientationMaskLandscapeLeft) {
                self.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
            }
            break;
            
        case UIDeviceOrientationPortraitUpsideDown:
            if (self.supportedInterfaceOrientations & UIInterfaceOrientationMaskPortraitUpsideDown) {
                self.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
            }
            break;
            
        case UIDeviceOrientationLandscapeRight:
            if (self.supportedInterfaceOrientations & UIInterfaceOrientationMaskLandscapeRight) {
                self.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
            }
            break;
            
        default:
            break;
    }
}

//屏幕旋转的布局处理
- (void)layoutSubviews {
    [super layoutSubviews];

    switch (self.style) {
//        case MOAlerterStyleDefault:
        case MOAlerterStyleInput: {
            [self.containView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_lessThanOrEqualTo(self.mas_height);
            }];
        }
            break;

        case MOAlerterStyleSheet: {
//            CGFloat table_H = fmin(55 * _titles.count + 16, [[UIScreen mainScreen] bounds].size.height - (iPhoneX ? 66 : 55) - IPHONE_NAVIGATIONBAR_HEIGHT - 8);
//            if ([_tableView isKindOfClass:[MOAlerterTableView class]]) {
//                table_H += [(MOAlerterTableView *)_tableView heightForHeader];
//            }
//            CGFloat height = table_H - 8 + (iPhoneX ? 66 : 55);
//            self.containView.frame = CGRectMake(TABLE_SPACE, [[UIScreen mainScreen] bounds].size.height - self.containView.height, [[UIScreen mainScreen] bounds].size.width - TABLE_SPACE * 2, height);
        }
            break;
            
        default:
            break;
    }
}


#pragma mark - 更新右边按钮的标题
- (void)makeRightBtnTitle:(NSString *)title {
    [self.rightBtn setTitle:title forState:(UIControlStateNormal)];
}

- (void)makeRightBtnEnabled:(BOOL)enabled {
    [self.rightBtn setEnabled:enabled];
}


#pragma mark - 异地登录，隐藏弹窗
- (void)loginByOtherDevice {
    if (self.hideWhenLoginByOtherDevice) {
        [self hide];
    }
}


@end

